import permission from "./permission";
import end from "./end";
import type { Router } from "vue-router";

const guards = {
  beforeEach: [permission],
  afterEach: [end],
};

export default function useRouterGuard(router: Router) {
  // 加载路由守卫
  const { beforeEach, afterEach } = guards;
  beforeEach.forEach((guard) => {
    if (guard && typeof guard === "function") {
      router.beforeEach((to, from) => guard(to, from, router));
    }
  });
  afterEach.forEach((guard) => {
    if (guard && typeof guard === "function") {
      router.afterEach((to, from) => guard(to, from, router));
    }
  });
}
